/*!
 * 面板控件.
 * ecp.qzz.ui.panel.js
 * 创建：QZZ
 * 日期：2014-03-01
 */
define(["qzz.window"], function() {
	
	var qzz = window.qzz;
	qzz.space("ui");
	/**
	 * 面板控件.
	 */
	qzz.ui.panel = qzz.Class(qzz.ui.window, {
		/**
		 * 创建函数
		 */
	    _create:function() {
	    	this.base();
            this.className = "Panel";
            //this.logInfo("Panel.create");      
            //对齐属性
            this.align = "alNone";
		    this.isContainer = true;
	    },
	    /**
	     * 渲染.
	     */
	    _render:function() {
	    	this._update = false;
	    	this.base();
	    	this._update = true;
	    	//如果父层是控件对象则用绝对定位样式
	    	if(this.isNotEmpty(this.parent) && this.getOption().Align != null) {
	    		if(this.parent.isComponent == null
	    			&& this.isEmpty(this.parent[0].style.position)) {
		    		this.parent[0].style.position = "relative";
	    		}
				//设置position的样式开开关
	    		if(this.getOption().absolute !== false) {
	    			this.thisWindow.style.position = "absolute";
	    		}	    		
	    	}	    	
	    	//设置对齐方式
	    	if(this.getOption().Align != null) {
	    	    this.setAlign(this.getOption().Align);
	    	}
	    },
	    /**
	     * 渲染后函数.
	     */
	    _afterRender:function(){
	    	//this.logInfo("Panel.afterRender");
	    	var _this = this;
	    	if(this.isNotEmpty(this.parent) &&
	    			!this.parent.isComponent) {
	    		//如果父控件是body，则添加onResize事件
    			var _this = this;
    			var _lock = false;
    			var _parent = this.parent[0];
    			if(_parent.hasBindResize == null && false) {
	    			this.parent.bind("resize" ,function() {
	    				var opt = _this.getOption();
	    				var cw = _this.getWidth()  
	    			       + _this._getMarginLeft() + _this._getMarginRight();
	    				var ch = _this.getHeight() 
	    			       + _this._getMarginTop() + _this._getMarginBottom();
	    				if(_lock || (opt.autoEcpWidth == false || _this.parent.width() == cw) 
	    						  && (opt.autoEcpHeight == false || _this.parent.height() == ch)
		    				        && (opt.Align == "alNone" || _this.isEmpty(opt.Align))) {
		    				     return false;
		    			}
	    				_lock = true;
	    				
	    				var pow = _parent.offsetWidth - _this._getPaddingLeft(_parent) - _this._getPaddingRight(_parent);
	    				var poh = _parent.offsetHeight - _this._getPaddingTop(_parent) - _this._getPaddingBottom(_parent);
	    				if(pow > 0 && poh > 0) { 
	    					_parent._width = pow;
	    					_parent._height = poh;
	    					if(opt.autoEcpWidth && (opt.Align == "alNone" || _this.isEmpty(opt.Align))) {
	    					    _this.setWidth(pow);
	    					} else {
	    				        _this._doResize(_parent);
	    					}
	    				}
	    				_lock = false;
	    			});
    			}    			
    			_parent.hasBindResize = true;
    		}
	    	this.base();
	    	if(this.getOption().absolute === false) {
	    		this.thisWindow.style.left = "";
	    	}
	    },
	    doResize:function() {
	    	this._doResize(this.parent[0]);
	    	if(this.getOption().absolute === false) {
	    		this.thisWindow.style.left = "";
	    	}
	    },
	    /**
	     * 执行大小变动.
	     */
	    _doResize:function(el) {
	    	if((this.getAlign() !== "alNone" || typeof this._width == "string" && this._width.indexOf("%")>0) && this.isShow() === false) return;
	    	if(!this._update) return false;	    	
	    	if(el == null) {
	    		//没有指定el的时候，处理当前控件的布局
	    		this.base();
		    	//处理变更事件，并自动处理子节点事件。
		    	if(typeof this.onResize == "function") {
		    		this.onResize(this.getWidth(), this.getHeight());
		    	}
		    	this.trigger("onResize", [this.getWidth(), this.getHeight()]);
	    	}
	    	this.logInfo("panel doResize");
	    	var pn = el || this;
	    	var len = pn.childComponent.length;
	    	if(len > 0) {
		    	//设置坐标	    	
		    	var lw = 0, 
		    	    rw = 0, 
		    	    th = 0, 
		    	    bh = 0;
		    	//获取父窗口的top, left, width, height, (el)
		    	var rectLeft = this._getRectLeft(el);
		    	var rectTop = this._getRectTop(el);
		    	
		    	var rectWidth = this._getRectWidth(el);
		    	var rectHeight = this._getRectHeight(el);
		    	
		    	var alcn = null;
		    	//处理所有子控件的对齐方式.		    	
		    	for(var i = 0; i < len; i++) {
		    		var cn = pn.childComponent[i];
		    		if(cn.isContainer) {
		    			//当前控件的外边距宽高
		    			var mh = cn._getMarginTop() + cn._getMarginBottom();
		    			var mw = cn._getMarginLeft() + cn._getMarginRight();
		    			//当前剩余的宽高
		    			var sh = rectHeight - th - bh;
		    			var sw = rectWidth - lw - rw;
		    			//顶/左偏移量
		    			var ot = rectTop;//rectTop + cn._getMarginTop();
		    			var ol = rectLeft;//rectLeft + cn._getMarginLeft();
		    			//只处理内部控件
		    			var al = cn.getAlign();
		    		    if(al === "alClient") {
		    		    	//全屏的留后处理
		    		    	alcn = cn;		    				
		    		    } else if(al === "alRight") {
		    		    	//靠右
		    		    	//当前宽度+外边距
		    		    	rw += cn.getWidth() + mw;
		    		    	//剩余高度 减去自己的外边框
		    		    	cn.setHeight(sh - mh, false);
		    		    	//已占用高度   + 偏移量
		    		    	cn.setTop(th + ot, false);
		    		    	//靠右，反向求值, 总宽度 - 右边已占用宽度  + 偏移量
		    		    	cn.setLeft(rectWidth - rw  + ol);
		    		    } else if(al === "alLeft") {
		    		    	cn.setTop(th + ot, false);
						    cn.setLeft(lw + ol, false);
		    		    	lw += cn.getWidth() + mw;
		    		    	cn.setHeight(sh - mh);	    			    	
		    		    } else if(al === "alTop") { 
		    		    	cn.setTop(th + ot, false);
						    cn.setLeft(lw + ol, false);
		    		    	th += cn.getHeight() + mh;
		    		    	cn.setWidth(sw - mw);
		    		    } else if (al === "alBottom") {
		    		    	bh += cn.getHeight() + mh;
		    		    	cn.setWidth(sw - mw, false);
		    		    	cn.setTop(rectHeight - bh + ot, false);
		    		    	cn.setLeft(lw + ol);
		    		    } else if(al === "alFull") {
		    		    	cn.setWidth("100%", false);
		    		    	cn.setHeight("100%");
		    		    }	    			
		    		}
		    		cn = null;
		    	}
		    	if(this.isNotEmpty(alcn)) {
		    		//当前控件的外边距宽高
	    			var mh = alcn._getMarginTop() + alcn._getMarginBottom();
	    			var mw = alcn._getMarginLeft() + alcn._getMarginRight();
	    			//当前剩余的宽高
	    			var sh = rectHeight - th - bh;
	    			var sw = rectWidth - lw - rw;
	    			//顶/左偏移量
	    			var ot = rectTop;//rectTop + cn._getMarginTop();
	    			var ol = rectLeft;//rectLeft + cn._getMarginLeft();
	    			
		    		//最后处理全屏的控件.
		    		alcn.setWidth(sw - mw, false);
		    		alcn.setLeft(lw + ol, false);
		    		alcn.setHeight(sh - mh, false);
		    		alcn.setTop(th + ot);
		    	}
		    	alcn = null;
	    	}
	    },
	    /**
	     * 获取对齐属性.
	     * @return 返回align
	     */
	    getAlign:function() {
	    	return this.align;
	    },
	    doShow:function(){
	    	this._doResize(this.parent[0]);
	    },
	    /**
	     * 设置对齐属性.
	     * @param algin 对齐方式
	     */
	    setAlign:function(align) {
	    	if(this.align !== align) {
	    	    this.align = align;
	    	    this._sizeChanged = true;
	    	    //this.doAlign();
	    	    if(this.isNotEmpty(this.parent)) {
				    if(this.parent.isComponent) {
				        this.parent._doResize();
					} else {
					    this._doResize(this.parent[0]); 
					}
				}
	    	}
	    }
	});
	
	/**
	 * 布局控件.
	 */
	_bindRenderFunc("qzzpanel", qzz.ui.panel);

	return qzz.ui.panel;
});